﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;
using YAF.Core;
using YAF.Utils;

public partial class script_migration : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from Users WHERE id>1 and id<540", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    while (dr.Read())
                    {
                        if (dr["username"].ToString() != "" && dr["password"].ToString() != "")
                        {
                            string pass = DAO.CodeFactory.CalcMD5String(dr["orig_pass"].ToString());
                            string _pass = dr["orig_pass"].ToString();
                            string user = dr["username"].ToString();
                            using (SqlCommand cmd1 = new SqlCommand("UPDATE Users SET password='" + pass + "' WHERE id=" + dr["id"].ToString(), conn))
                            {
                                //object v = cmd1.ExecuteNonQuery();
                                CreateUserForForum(user, _pass);
                            }
                        }
                    }
                }
                dr.Close();
            }
        }
    }

    private void CreateUserForForum(string username, string pass)
    {
        string hashinput = DateTime.UtcNow + pass + Security.CreatePassword(20);
        string hash = FormsAuthentication.HashPasswordForStoringInConfigFile(hashinput, "md5");

        MembershipCreateStatus status;
        MembershipUser user = Membership.CreateUser(
            username,
            pass.Trim(),
            username,
            "test",
            "test",
            true,
            null,
            out status);
        if (status != MembershipCreateStatus.DuplicateEmail)
        {
            RoleMembershipHelper.SetupUserRoles(YafContext.Current.PageBoardID, username);
            RoleMembershipHelper.AddUserToRole(username, "Registered");
            int? userID = RoleMembershipHelper.CreateForumUser(user, YafContext.Current.PageBoardID);
            YafUserProfile userProfile = YafUserProfile.GetProfile(username);
            userProfile.Save();
            Response.Write("User " + username + " added");
        }
        else
        {
            Response.Write("User " + username + " exists");
        }
        //LegacyDb.user_save(
        //    UserMembershipHelper.GetUserIDFromProviderUserKey(user.ProviderUserKey),
        //    YafContext.Current.PageBoardID,
        //    null,
        //    null,
        //    null,
        //    TimeZone.CurrentTimeZone.ToType<int>(),
        //    null,
        //    null,
        //    null,
        //    null,
        //    null,
        //    null,
        //    null,
        //    null,
        //    null,
        //    null,
        //    null);

        /*string strHash = FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "SHA1");
        object app = "YetAnotherForum";
        object objUserKey = YAF.Providers.Profile.DB.Current.GetProviderUserKey(app, (object)username);
        YAF.Providers.Membership.DB.Current.CreateUser(app,username,pass, "", 
        YAF.Providers.Membership.DB.Current.CreateUser(app, username, strHash, "", 0, username, "", "", true, objUserKey);
YAF.Providers.Roles.DB.AddUserToRole(strAppName, strUsername, "Registered")
        string guestUserName = UserMembershipHelper.GuestUserName;
        guestUserName = (guestUserName != null && guestUserName != "") ? guestUserName.ToLower() : String.Empty;

        RoleMembershipHelper.SetupUserRoles(YafContext.Current.PageBoardID, username);
         * */
    }
}
